﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrmApplication.Models;
using Oracle.DataAccess.Client;
using System.IO;

namespace CrmApplication.Controllers
{
    public class ContactController : Controller
    {
        //
        // GET: /Contact/

        public ActionResult Index()
        {
            return View();
        }

		/// <summary>
		/// get all contacts(persons)/ filter contacts 
		/// </summary>
		/// <returns></returns>
        public ActionResult GetContacts()
        {
            decimal idAge = 0;
            Decimal.TryParse(Request.Params["id_age"], out idAge);
            decimal idPos = 0;
            Decimal.TryParse(Request.Params["id_position"], out idPos);
            decimal idSubpos = 1;
            Decimal.TryParse(Request.Params["id_subposition"], out idSubpos);
            decimal idDiv = 0;
            Decimal.TryParse(Request.Params["id_division"], out idDiv);
            decimal idPriority = 0;
            Decimal.TryParse(Request.Params["id_priority"], out idPriority);
			decimal idCompany = 0;
			Decimal.TryParse(Request.Params["addCompany"], out idCompany);
			decimal idDomain = 0;
			Decimal.TryParse(Request.Params["addDomain"], out idDomain);
			decimal idSubdomain = 0;
			Decimal.TryParse(Request.Params["addSubdomain"], out idSubdomain);
			decimal idCity = 0;
			Decimal.TryParse(Request.Params["addCity"], out idCity);
			decimal idCounty = 0;
			Decimal.TryParse(Request.Params["addCounty"], out idCounty);
			decimal idEmployee = 0;
			Decimal.TryParse(Request.Params["employee"], out idEmployee);
			decimal idTurnover = 0;
			Decimal.TryParse(Request.Params["turnover"], out idTurnover);
			decimal idIncome = 0;
			Decimal.TryParse(Request.Params["id_income"], out idIncome);
            string firstName = Request.Params["FirstName"];
            string lastName = Request.Params["LastName"];
			string availableId = Request.Params["available"];
			string idSex = Request.Params["sex"];
			
			string stAddress = Request.Params["address"];
			
			ContactModels contact = new ContactModels();

			contact.IdCompany = idCompany;
			contact.IdDomain = idDomain;
			contact.IdSubdomain = idSubdomain;
			contact.IdCounty = idCounty;
			contact.IdCity = idCity;
			contact.IdEmployee = idEmployee;
			contact.IdTurnover = idTurnover;
			contact.IdAge = idAge;
			contact.IdPosition = idPos;
			contact.IdSubPosition= idSubpos;
			contact.IdDivision = idDiv;
			contact.IdIncome = idIncome;
			contact.IdPriority = idPriority;
			contact.FirstName = Request.Params["FirstName"];
			contact.LastName = Request.Params["LastName"];
			contact.Available = Request.Params["available"];
			if (idSex == "1")
			{
				contact.Sex = "F";
			}
			else
				if (idSex == "2")
				{
					contact.Sex = "M";
				}			

            ContactContainer container = ContactModels.GetContactArray(contact);
            return Json(container, JsonRequestBehavior.AllowGet);
        }

		/// <summary>
		/// get info for selected person
		/// </summary>
		/// <returns></returns>
		public ActionResult GetContactById ()
		{
			decimal idContact = 0;
			Decimal.TryParse(Request.Params["person_id"], out idContact);

			ContactContainer container = ContactModels.GetContactArrayById(idContact);
			return Json(container, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// select all contacts from a campaign
		/// </summary>
		/// <returns></returns>
		public ActionResult GetContactByCampaign ()
		{
			decimal idCampaign = 0;
			Decimal.TryParse(Request.Params["IdCampaign"], out idCampaign);
			decimal idPos = 0;
			Decimal.TryParse(Request.Params["IdPosition"], out idPos);
			decimal idSubpos = 1;
			Decimal.TryParse(Request.Params["IdSubposition"], out idSubpos);
			decimal idCompany = 0;
			Decimal.TryParse(Request.Params["IdCompany"], out idCompany);
			decimal idCity = 0;
			Decimal.TryParse(Request.Params["IdCity"], out idCity);
			decimal idCounty = 0;
			Decimal.TryParse(Request.Params["IdCounty"], out idCounty);

			string idStatus = Request.Params["IdStatus"];
			string status = "";
			if ((idStatus != "") & (idStatus != null))
			{
				status = idStatus.Replace("=", ",");
			}

			string statusType = Request.Params["StatusType"];
			string sType = "";
			if ((statusType != "") & (statusType != null))
			{
				if (statusType == "1")
				{
					sType = " in ";
				}
				else
					if (statusType == "0")
					{
						sType = " not in ";
					}
			}

			ContactModels contact = new ContactModels();
			contact.IdCampaign = idCampaign;
			contact.IdCompany = idCompany;			
			contact.IdCounty = idCounty;
			contact.IdCity = idCity;			
			contact.IdPosition = idPos;
			contact.IdSubPosition = idSubpos;			
			contact.FirstName = Request.Params["FirstName"];
			contact.LastName = Request.Params["LastName"];

			ContactContainer container = ContactModels.GetContactArrayByIdCampaign(contact, status, sType);
			return Json(container, JsonRequestBehavior.AllowGet);
		}
		
		/// <summary>
		/// save/edit a contact infos
		/// </summary>
		/// <returns></returns>
		public ActionResult SaveContact ()
		{
			ContactModels contact = new ContactModels();

			decimal idPos = 0;
			Decimal.TryParse(Request.Params["IdPosition"], out idPos);
			decimal idSubpos = 0;
			Decimal.TryParse(Request.Params["IdSubposition"], out idSubpos);
			decimal idAge = 0;
			Decimal.TryParse(Request.Params["IdAge"], out idAge);
			decimal idDiv = 0;
			Decimal.TryParse(Request.Params["IdDivision"], out idDiv);
			decimal idPriority = 0;
			Decimal.TryParse(Request.Params["IdPriority"], out idPriority);
			decimal idCompany = 0;
			Decimal.TryParse(Request.Params["IdCompany"], out idCompany);
			decimal idDomain = 0;
			Decimal.TryParse(Request.Params["IdDomain"], out idDomain);
			decimal idSubdomain = 0;
			Decimal.TryParse(Request.Params["IdSubdomain"], out idSubdomain);
			decimal idBranch = 0;
			Decimal.TryParse(Request.Params["IdBranch"], out idBranch);

			string sex = Request.Params["sex"];
			if (sex == "1")
			{
				contact.Sex = "F";
			}
			else
				if (sex == "2")
				{
					contact.Sex = "M";
				}
			
			string available = Request.Params["Available"];
			
			string personId = Request.Params["person_id"];
			decimal persId = 0;
			Decimal.TryParse(Request.Params["person_id"], out persId);

			contact.IdContact = persId;
			contact.FirstName = Request.Params["FirstName"];
			contact.LastName = Request.Params["LastName"];
			contact.PhoneNumber = Request.Params["PhoneNumber"];
			contact.MobilNumber = Request.Params["MobilNumber"];
			contact.Email1 = Request.Params["Email1"];
			contact.Email2 = Request.Params["email2"];
			contact.WebAddress = Request.Params["WebAddress"];
			contact.IdPosition = idPos;
			contact.IdSubPosition = idSubpos;
			contact.IdDivision = idDiv;
			contact.IdPriority = idPriority;
			contact.IdCompany = idCompany;
			contact.IdAge = idAge;
			contact.IdDomain = idDomain;
			contact.IdSubdomain = idSubdomain;
			contact.Available = available;
			contact.Observations=Request.Params["obs"];
			contact.IdBranch = idBranch;				
			//contact.BirthDate = Convert.ToDateTime(Request.Params["birth_date"]);	
			
			string messageBack = null;
			try
			{				
				if(persId>0)
				{
					ContactModels.EditContact(contact);
					messageBack = "Contactul a fost modificat cu succes!";
				}
				else
					if (personId.Substring(0, 3) == "new")
					{
						ContactModels.AddContact(contact);
						messageBack = "Contactul a fost adaugat cu succes!";
					}

				return Json(new { success = true, description = messageBack });
			}
			catch (Exception ex)
			{
				return null;
			}    
		}

		/// <summary>
		/// export persons informations into Excel(can use filters)
		/// </summary>
		/// <returns></returns>
		public ActionResult Export ()
		{
			decimal idAge = 0;
			Decimal.TryParse(Request.Params["id_age"], out idAge);
			decimal idPos = 0;
			Decimal.TryParse(Request.Params["id_position"], out idPos);
			decimal idSubpos = 1;
			Decimal.TryParse(Request.Params["id_subposition"], out idSubpos);
			decimal idDiv = 0;
			Decimal.TryParse(Request.Params["id_division"], out idDiv);
			decimal idPriority = 0;
			Decimal.TryParse(Request.Params["id_priority"], out idPriority);
			decimal idCompany = 0;
			Decimal.TryParse(Request.Params["addCompany"], out idCompany);
			decimal idDomain = 0;
			Decimal.TryParse(Request.Params["addDomain"], out idDomain);
			decimal idSubdomain = 0;
			Decimal.TryParse(Request.Params["addSubdomain"], out idSubdomain);
			decimal idCity = 0;
			Decimal.TryParse(Request.Params["addCity"], out idCity);
			decimal idCounty = 0;
			Decimal.TryParse(Request.Params["addCounty"], out idCounty);
			decimal idEmployee = 0;
			Decimal.TryParse(Request.Params["employee"], out idEmployee);
			decimal idTurnover = 0;
			Decimal.TryParse(Request.Params["turnover"], out idTurnover);
			decimal idIncome = 0;
			Decimal.TryParse(Request.Params["id_income"], out idIncome);
			string firstName = Request.Params["FirstName"];
			string lastName = Request.Params["LastName"];			
			string idSex = Request.Params["sex"];

			string stAddress = Request.Params["address"];

			ContactModels contact = new ContactModels();

			contact.IdCompany = idCompany;
			contact.IdDomain = idDomain;
			contact.IdSubdomain = idSubdomain;
			contact.IdCounty = idCounty;
			contact.IdCity = idCity;
			contact.IdEmployee = idEmployee;
			contact.IdTurnover = idTurnover;
			contact.IdAge = idAge;
			contact.IdPosition = idPos;
			contact.IdSubPosition = idSubpos;
			contact.IdDivision = idDiv;
			contact.IdIncome = idIncome;
			contact.IdPriority = idPriority;
			contact.FirstName = Request.Params["FirstName"];
			contact.LastName = Request.Params["LastName"];
			contact.Available = Request.Params["available"];
			
			if (idSex == "1")
			{
				contact.Sex = "F";
			}
			else
				if (idSex == "2")
				{
					contact.Sex = "M";
				}

			List<ContactModels> allContacts = new List<ContactModels>();
			allContacts = ContactModels.GetContacts(contact);

			StringWriter sw = new StringWriter();
			
			sw.WriteLine("<table><tr><td>Nume</td><td>Prenume</td><td>Sex</td><td>Functie</td><td>Subfunctie</td><td>Companie</td><td>Adresa</td><td>Oras</td></tr>");

			foreach (ContactModels item in allContacts)
			{
				sw.WriteLine("<tr><td>"+item.LastName+"</td><td>"+item.FirstName+"</td><td>"+item.Sex+"</td><td>"+item.PositionName+"</td><td>"+item.SubPositionName+"</td><td>"+item.CompanyName+"</td><td>"+item.Address+"</td><td>"+item.IdCity_val+"</td></tr>");
			}

			sw.WriteLine("</table>");			

			Response.ClearContent();
			Response.AddHeader("Content-Disposition", "attachment; filename=Contacts.xls");
			Response.ContentType = "application/ms-excel";			
			Response.Write(sw);
			Response.End();

			return null;
		}
		
    }
}
